package com.telenav;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class MedianDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_width;
	private Text text_height;
	private Text text_centerx;
	private Text text_centery;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public MedianDialog(Shell parent, int style) {
		super(parent, style);
		setText("Median Filter");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			
			StringBuffer sb = new StringBuffer();
			sb.append(((text_width.getText()==null || text_width.getText().equals(""))?("3"):text_width.getText())+";");
			sb.append(((text_height.getText()==null || text_height.getText().equals(""))?("3"):text_height.getText())+";");
			sb.append(((text_centerx.getText()==null || text_centerx.getText().equals(""))?("1"):text_centerx.getText())+";");
			sb.append(((text_centery.getText()==null || text_centery.getText().equals(""))?("1"):text_centery.getText()));
			result = sb.toString();
			
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(204, 320);
		shell.setText(getText());
		shell.setLayout(null);

		Group grpSmoothType = new Group(shell, SWT.NONE);
		grpSmoothType.setText("Smooth Type");
		grpSmoothType.setBounds(10, 10, 126, 125);

		Button radio_31 = new Button(grpSmoothType, SWT.RADIO);
		radio_31.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelect31();
			}
		});
		radio_31.setBounds(10, 24, 90, 16);
		radio_31.setText("3x1");

		Button radio_13 = new Button(grpSmoothType, SWT.RADIO);
		radio_13.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelect13();
			}
		});
		radio_13.setBounds(10, 55, 90, 16);
		radio_13.setText("1x3");
		
		Button radio_33 = new Button(grpSmoothType, SWT.RADIO);
		radio_33.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelect33();
			}
		});
		radio_33.setBounds(10, 86, 90, 16);
		radio_33.setText("3x3");

		Label lblWidth = new Label(shell, SWT.NONE);
		lblWidth.setBounds(9, 168, 54, 15);
		lblWidth.setText("Width");

		Label lblHeight = new Label(shell, SWT.NONE);
		lblHeight.setBounds(9, 200, 54, 15);
		lblHeight.setText("Height");

		Label lblCenterX = new Label(shell, SWT.NONE);
		lblCenterX.setBounds(9, 234, 54, 15);
		lblCenterX.setText("Center X");

		Label lblCenterY = new Label(shell, SWT.NONE);
		lblCenterY.setBounds(9, 265, 54, 15);
		lblCenterY.setText("Center Y");

		text_width = new Text(shell, SWT.BORDER);
		text_width.setBounds(63, 168, 48, 21);

		text_height = new Text(shell, SWT.BORDER);
		text_height.setBounds(63, 200, 48, 21);

		text_centerx = new Text(shell, SWT.BORDER);
		text_centerx.setBounds(63, 234, 48, 21);

		text_centery = new Text(shell, SWT.BORDER);
		text_centery.setBounds(63, 265, 48, 21);
		
		doSelect31();

	}

	private void doSelect31() {
		text_width.setText("1");
		text_height.setText("3");
		text_centerx.setText("0");
		text_centery.setText("1");
	}

	private void doSelect13() {
		text_width.setText("3");
		text_height.setText("1");
		text_centerx.setText("1");
		text_centery.setText("0");
	}

	private void doSelect33() {
		text_width.setText("3");
		text_height.setText("3");
		text_centerx.setText("1");
		text_centery.setText("1");
	}
}
